//
//  HGBusinessVC.m
//  RedWinMall
//
//  Created by HeDianZhao on 2018/8/8.
//  Copyright © 2018年 HeDianZhao. All rights reserved.
//

#import "HGBusinessVC.h"
#import "QQAddressVC.h"
#import "BRStringPickerView.h"
#import "XZCustomCamer.h"

@interface HGBusinessVC ()<UITextFieldDelegate>

@property (strong, nonatomic) UIImageView *bgImageView;

@property (strong, nonatomic) UIView *bgView;

@property (strong, nonatomic) UIImageView *uploadImgView;

@property (strong, nonatomic) UIView *infoView;

@property (strong, nonatomic) UIImage *seletedImage;

@property (strong, nonatomic) UIImageView *ggImg;

@property (copy, nonatomic) NSMutableArray <UITextField *> *textFields;


@property (strong, nonatomic) UIButton *uploadBtn;

@property (strong, nonatomic) UIButton *photoBtn;
@end

@implementation HGBusinessVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"代理商申请";
    
    self.view.backgroundColor = kColorBackground;
    
    UIImageView *topBgImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dl_bg"]];
    topBgImgView.userInteractionEnabled = YES;
    [self.view addSubview:topBgImgView];

    [topBgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    UIImageView *logoImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dl_logo"]];
    [topBgImgView addSubview:logoImg];
    
    [logoImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.offset(15);
    }];
    
    _ggImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wenan"]];
    [topBgImgView addSubview:_ggImg];
    
    [_ggImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(logoImg.mas_bottom).offset(kNavigationHeight);
    }];

    _infoView = [[UIView alloc] init];
    _infoView.layer.cornerRadius = 10;
    _infoView.layer.masksToBounds = YES;
    _infoView.layer.borderColor = [UIColor whiteColor].CGColor;
    _infoView.layer.borderWidth = 1.5;
    [self.view addSubview:_infoView];
    [_infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(_ggImg.mas_bottom).offset(20);
        make.size.mas_equalTo(CGSizeMake(320 * kScale, (57 *6) * kScale));
    }];

    NSArray *leftTitle = @[@"联系人:",@"手机号:",@"申请等级:",@"所在地区:",@"",@""];
    NSArray *placeholderText = @[@"请输入名字",@"请输入手机号码",@"选择代理商类型",@"请选择地址",@"请输入详细地址"];

    for (int i = 0; i < leftTitle.count; i++) {
        UIView *bgView = [[UIView alloc] init];
        bgView.layer.masksToBounds = YES;
        [_infoView addSubview:bgView];
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_infoView).offset(i * 57);
            make.left.right.equalTo(_infoView);
            make.height.offset(57);
        }];
        
        if (i == 5) {
            UIButton *sumbit = [[UIButton alloc] init];
            [sumbit setTitle:@"下一步" forState:(UIControlStateNormal)];
            [sumbit addTarget:self action:@selector(sumbitData:) forControlEvents:(UIControlEventTouchUpInside)];
            [bgView addSubview:sumbit];
            
            [sumbit mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(bgView);
            }];
            continue;
        }

        UILabel *leftLabel = [[UILabel alloc] init];
        leftLabel.text = leftTitle[i];
        leftLabel.textColor = [UIColor whiteColor];
        [bgView addSubview:leftLabel];

        [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(bgView);
            make.left.offset(10);
            make.width.offset(80);
        }];

        UITextField *textField = [[UITextField alloc] init];
        textField.tag = 1000 + i;
        textField.delegate = self;
        textField.textColor = kColorRGBAlpha(225, 225, 225, 0.5);
        [textField setValue:kColorRGBAlpha(225, 225, 225, 0.5) forKeyPath:@"_placeholderLabel.textColor"];
        textField.placeholder = placeholderText[i];
        [self.textFields addObject:textField];
        [bgView addSubview:textField];

        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(leftLabel);
            make.top.right.bottom.equalTo(bgView);
            i != 4 ? make.left.equalTo(leftLabel.mas_right).offset(10) : make.left.offset(10);
        }];
        
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = [UIColor whiteColor];
        [bgView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.offset(1);
            make.left.right.bottom.equalTo(bgView);
        }];
    }

    self.uploadImgView.hidden = YES;
    self.bgImageView.hidden = NO;
}


#pragma mark - UITextFieldDelegate 返回键
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    if (textField.tag == 1002 || textField.tag == 1003) {
        [self.view endEditing:YES];
        [self handlerTextFieldSelect:textField];
        return NO;
    }
    return YES;
}

#pragma mark - 调用地址选择器 本地 有时间改成线上
- (void)handlerTextFieldSelect:(UITextField *)textField {
    if (textField.tag == 1003) {
        QQAddressVC *vc = [QQAddressVC new];
        vc.returnData = ^(NSString *city, NSString *cityS) {
            textField.text = city;
        };
        vc.modalPresentationStyle = UIModalPresentationCustom;
        [self presentViewController:vc animated:YES completion:nil];
        return;
    }
    
    
    [BRStringPickerView showStringPickerWithTitle:@"代理商类型" dataSource:@[@"皇家体验馆(一次性购酒50W)",@"名流体验馆(一次性购酒30W)",@"风尚体验馆(一次性购酒10W)"] defaultSelValue:nil resultBlock:^(id selectValue, NSInteger index) {
        textField.text = selectValue;
    }];
}

#pragma mark - 提交
- (void)sumbitData:(UIButton *)sender {
    
    //先验证
    __block BOOL isCertification = YES;
    [_textFields enumerateObjectsUsingBlock:^(UITextField * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([Utility isBlankString:obj.text]) isCertification = NO;
    }];
    if (isCertification == NO) {
        [[XNProgressHUD shared] showErrorWithTitle:@"信息未完善" maskType:(XNProgressHUDMaskTypeBlack)];
        return;
    }
    
    //验证通道隐藏
    self.uploadImgView.hidden = NO;
    _uploadBtn.hidden = NO;
    self.infoView.hidden = YES;
    _ggImg.hidden = YES;
    _photoBtn.hidden = NO;
}

#pragma mark - 上传
- (void)upload {
    if (_seletedImage != nil) {
        
        [[XNProgressHUD shared] showLoadingWithTitle:@"上传中" maskType:(XNProgressHUDMaskTypeBlack)];
        NSDictionary *parameters = @{
                                     @"name" :_textFields[0].text,
                                     @"tel" : _textFields[1].text,
                                     @"address" : _textFields[3].text,
                                     @"applyType" : @1
                                     };
        
        __block NSData *data = [Utility compressOriginalImage:_seletedImage toMaxDataSizeKBytes:512];
        
        [[LMJRequestManager sharedManager] upload:KBaseURL(@"app/agentApply/submit") parameters:parameters formDataBlock:^(id<AFMultipartFormData> formData) {
            NSString *fileName = [NSString stringWithFormat:@"%@", kQQUserInfoManger.userId];
            [formData appendPartWithFileData:data name:@"file" fileName:[NSString stringWithFormat:@"%@.jpg", fileName] mimeType:@"image/jpeg"];
        } progress:^(NSProgress *progress) { } completion:^(LMJBaseResponse *response) {
            [[XNProgressHUD shared] dismiss];
            if (response.responseModel.status == 1)
            {
                self.bgView.hidden = NO;
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }
            else
            {
                
            }
        }];
    }
}

- (void)pushAction {
    _bgImageView.hidden = YES;
}

#pragma mark - 上传证明
- (void)uploadImage:(UIButton *)sender {
    
    WS(weakSelf)
    [LEEAlert actionsheet].config
    .LeeContent(@"请上传汇款凭证")
    .LeeAction(@"相机", ^{
        [[XZCustomCamer shareCustom] pushPhotoAlbumcholoes:(XZCustomCamerTypeCamer) setPhotoBlock:^(UIImage *image) {
            weakSelf.seletedImage = image;
            [sender setImage:image forState:UIControlStateNormal];
        }];
    })
    .LeeAction(@"从相册中选取", ^{
        
        [[XZCustomCamer shareCustom] pushPhotoAlbumcholoes:(XZCustomCamerTypePhoto) setPhotoBlock:^(UIImage *image) {
            weakSelf.seletedImage = image;
            [sender setImage:image forState:UIControlStateNormal];
        }];
    })
    .LeeCancelAction(@"取消", ^{
        
    })
    .LeeShow();
}


- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_bgView];
        
        [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
        
        UIImageView *topBgImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-s"]];
        [_bgView addSubview:topBgImgView];
        
        [topBgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self.view);
        }];
        
        UIImageView *bottomBgImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg-x"]];
        [_bgView addSubview:bottomBgImgView];
        
        [bottomBgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.equalTo(self.view);
        }];
        
        UIImageView *centerImgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tjcg"]];
        [_bgView addSubview:centerImgV];
        
        [centerImgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.view);
        }];
        
        UILabel *label = [[UILabel alloc] init];
        label.text = @"请耐心等候，客服将在1-3个\n工作日内与您联系...";
        label.numberOfLines = 2;
        label.font = [UIFont systemFontOfSize:14];
        label.textAlignment = NSTextAlignmentCenter;
        [centerImgV addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(centerImgV);
            make.centerY.equalTo(centerImgV).multipliedBy(1.2);
            make.top.offset(70);
        }];
        
    }
    return _bgView;
}



- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        
        _bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"zsjh"]];
        _bgImageView.userInteractionEnabled = YES;
        [self.view addSubview:_bgImageView];
        
        [_bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
        
        UIButton *sender = [[UIButton alloc] init];
        [sender setBackgroundImage:[UIImage imageNamed:@"anniu"] forState:(UIControlStateNormal)];
        sender.titleLabel.font = [UIFont systemFontOfSize:12];
        [sender setTitle:@"开启体验馆之旅" forState:(UIControlStateNormal)];
        [sender addTarget:self action:@selector(pushAction) forControlEvents:(UIControlEventTouchUpInside)];
        [_bgImageView addSubview:sender];
        
        [sender mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_bgImageView).offset(-60 * kScale);
            make.centerX.equalTo(_bgImageView);
        }];
    }
    return _bgImageView;
}

- (UIImageView *)uploadImgView {
    if (!_uploadImgView) {
        _uploadImgView = [[UIImageView alloc] init];
        _uploadImgView.backgroundColor = kColorRGBAlpha(225, 225, 225, 0.5);
        _uploadImgView.userInteractionEnabled = YES;
        [self.view addSubview:_uploadImgView];
        
        CGFloat font = 14;
        
        UILabel *infoLabel = [[UILabel alloc] init];
        infoLabel.text = @"收款人名称: 深圳市巧巧科技有限公司\n收款人账户: 15000090729465\n开户行: 平安银行深圳龙华支行";
        infoLabel.numberOfLines = 0;
        infoLabel.font = [UIFont systemFontOfSize:font];
        infoLabel.textColor = [UIColor whiteColor];
        infoLabel.textAlignment = NSTextAlignmentCenter;
        infoLabel.backgroundColor = kColorWithFloat(0xa6212c);
        [_uploadImgView addSubview:infoLabel];
        
        [infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_uploadImgView);
            make.top.equalTo(_uploadImgView).offset(15);
            make.size.mas_equalTo(CGSizeMake(300, 111));
        }];
        
        UILabel *leftLabel = [[UILabel alloc] init];
        leftLabel.text = @"请按以上账户信息进行汇款并上传真实有效的汇款凭证：";
        leftLabel.font = [UIFont systemFontOfSize:font];
        leftLabel.textColor = [UIColor whiteColor];
        [_uploadImgView addSubview:leftLabel];
        
        [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_uploadImgView);
            make.top.equalTo(_uploadImgView.mas_bottom).offset(30);
        }];
        
        _photoBtn = [[UIButton alloc] init];
        _photoBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        [_photoBtn setImage:[UIImage imageNamed:@"tupian"] forState:(UIControlStateNormal)];
        [_photoBtn addTarget:self action:@selector(uploadImage:) forControlEvents:(UIControlEventTouchUpInside)];
        _photoBtn.hidden = YES;
        [self.view addSubview:_photoBtn];
        
        [_photoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(leftLabel);
            make.top.equalTo(leftLabel.mas_bottom).offset(30);
            make.size.mas_equalTo(CGSizeMake(200, 150));
        }];
        
        [_uploadImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self.view);
            make.height.offset(150);
        }];
        
        
        _uploadBtn = [[UIButton alloc] init];
        [_uploadBtn setTitle:@"上传" forState:(UIControlStateNormal)];
        _uploadBtn.layer.cornerRadius = 10;
        _uploadBtn.layer.borderWidth = 1;
        _uploadBtn.hidden = YES;
        _uploadBtn.layer.borderColor = [UIColor whiteColor].CGColor;
        [_uploadBtn addTarget:self action:@selector(upload) forControlEvents:(UIControlEventTouchUpInside)];
        [self.view addSubview:_uploadBtn];
        
        [_uploadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_photoBtn);
            make.top.equalTo(_photoBtn.mas_bottom).offset(50);
            make.size.mas_equalTo(CGSizeMake(kScreenWidth - 60, 48));
        }];
    }
    return _uploadImgView;
}

- (NSMutableArray<UITextField *> *)textFields {
    if (!_textFields) {
        _textFields = [@[] mutableCopy];
    }
    return _textFields;
}

@end
